﻿<UserControl
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
    xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity" 
    xmlns:n="http://nRoute/schemas/2010/xaml"
    mc:Ignorable="d"  xmlns:Behaviors="clr-namespace:Orktane.Behaviors" 
    x:Class="Orktane.Controls.ConsoleWindow" 
    d:DesignWidth="750" d:DesignHeight="480" MaxWidth="750" MinWidth="750" 
    HorizontalAlignment="Left" MinHeight="300">
    
	<UserControl.Resources>
	
		<ResourceDictionary>
			<ResourceDictionary.MergedDictionaries>
				<ResourceDictionary Source="../Resources/ConsoleResourceDictionary.xaml"/>
			</ResourceDictionary.MergedDictionaries>
		</ResourceDictionary>
			
	</UserControl.Resources>
    
    <Grid x:Name="LayoutRoot" Background="{Binding BackgroundColor, Converter={StaticResource ConsoleColorValueConverter}, Mode=OneWay}">
    	
        <Grid.Effect>
    		<DropShadowEffect Direction="0"/>
    	</Grid.Effect>
        
    	<i:Interaction.Triggers>
    		<n:ReverseCommandTrigger ReverseCommand="{Binding BeepReverseCommand, Mode=OneWay}">
    			<Behaviors:PlayMediaElementAction TargetName="BeepAudioMedia"/>
    		</n:ReverseCommandTrigger>
            <n:KeyTrigger Key="C" WithControlModifier="True">
    			<n:ExecuteCommandAction Command="{Binding CancelConsoleCommand, Mode=OneWay}"/>
    		</n:KeyTrigger>
    	</i:Interaction.Triggers>
        
		<ScrollViewer x:Name="ConsoleScrollViewer" BorderBrush="{x:Null}" Padding="0"
			VerticalScrollBarVisibility="Auto" BorderThickness="0" 
			Style="{StaticResource ConsoleScrollViewerStyle}" Margin="0,30,0,0" IsTabStop="False">
			<StackPanel MinWidth="725">
                
				<TextBox x:Name="OutputTextBox" ToolTipService.Placement="Top" 
					Style="{StaticResource ConsoleTextBoxStyle}"
					Background="{x:Null}" BorderThickness="10,0,0,0" FontFamily="Courier New" 
					FontSize="14.667" IsReadOnly="True" TextWrapping="Wrap"
					SelectionBackground="#FF870000" Width="721" HorizontalAlignment="Left" 
					BorderBrush="#FF4FC945" Margin="0" Padding="5,0,0,0" 
					Text="{Binding ConsoleOutputText, Mode=OneWay}" 
					Foreground="{Binding ForegroundColor, Converter={StaticResource ConsoleColorValueConverter}, Mode=OneWay}">
					<i:Interaction.Triggers>
						<n:KeyTrigger Key="C" WithControlModifier="True">
							<n:ExecuteCommandAction Command="{Binding CancelConsoleCommand, Mode=OneWay}"/>
						</n:KeyTrigger>
					</i:Interaction.Triggers>
				</TextBox>
                
				<TextBox x:Name="DisabledInputTextBox" Style="{StaticResource ConsoleTextBoxStyle}"
					Background="{x:Null}" BorderBrush="Red" BorderThickness="10,0,0,0" 
					FontFamily="Courier New" FontSize="14.667" 
					SelectionBackground="#FF870000" TextWrapping="Wrap" CaretBrush="#FF3156FF" 
					Width="721" HorizontalAlignment="Left" Padding="5,0,0,0" IsReadOnly="True" Text="{Binding DisabledInputText, Mode=OneWay}" 
					Foreground="{Binding ForegroundColor, Converter={StaticResource ConsoleColorValueConverter}, Mode=OneWay}">
					<i:Interaction.Behaviors>
						<n:BoolValueVisibilityBehavior Value="{Binding DisableInput, Mode=OneWay}" Negate="False"/>
					</i:Interaction.Behaviors>
				</TextBox>
                
				<TextBox x:Name="InputTextBox" Style="{StaticResource ConsoleTextBoxStyle}"
					Background="{x:Null}" BorderBrush="#FF3156FF" BorderThickness="10,0,0,0" 
					FontFamily="Courier New" FontSize="14.667" 
					SelectionBackground="#FF870000" TextWrapping="Wrap" CaretBrush="#FF3156FF" 
					Width="721" HorizontalAlignment="Left" Margin="0,0,0,50" Padding="5,0,0,0"
					Text="{Binding ConsoleInputText, Mode=TwoWay, UpdateSourceTrigger=Explicit}" 
					Foreground="{Binding ForegroundColor, Converter={StaticResource ConsoleColorValueConverter}, Mode=OneWay}">
					<i:Interaction.Triggers>
						<Behaviors:InputConsoleKeyInfoTrigger>
							<Behaviors:UpdateValueRelayAction ValueRelay="{StaticResource ConsoleKeyInfoRelay}"/>
						</Behaviors:InputConsoleKeyInfoTrigger>
						<i:EventTrigger EventName="TextChanged">
							<n:UpdateBindingExplicitlyAction PropertyName="Text"/>
						</i:EventTrigger>
						<n:KeyTrigger Key="Enter">
							<n:ExecuteCommandAction 
								Command="{Binding ReadLineCommand, Mode=OneWay}" 
								Parameter="{Binding Text, ElementName=InputTextBox, Mode=OneWay}"/>
						</n:KeyTrigger>
                        <n:KeyTrigger KeyEvent="KeyDown" Key="Down" SetIsHandled="true">
                            <n:ExecuteCommandAction Command="{Binding GetNextInputCommand, Mode=OneWay}"/>
                            <Behaviors:UpdateCursorAction CursorPosition="End" />
                        </n:KeyTrigger>
                        <n:KeyTrigger KeyEvent="KeyDown" Key="Up" SetIsHandled="true">
                            <n:ExecuteCommandAction Command="{Binding GetPreviousInputCommand, Mode=OneWay}" />
                            <Behaviors:UpdateCursorAction CursorPosition="End" />
                        </n:KeyTrigger>
                        <n:KeyTrigger KeyEvent="KeyDown" Key="Escape" SetIsHandled="true">
                            <n:ExecuteCommandAction Command="{Binding ClearInputCommand, Mode=OneWay}" />
                        </n:KeyTrigger>
                        <n:KeyTrigger Negate="True" KeyEvent="KeyDown">
							<n:ExecuteCommandAction 
								Command="{Binding ReadKeyCommand, Mode=OneWay}" 
								Parameter="{Binding Value, Mode=OneWay, Source={StaticResource ConsoleKeyInfoRelay}}"/>
						</n:KeyTrigger>
						<!--<n:KeyTrigger Negate="True">
							<n:SetPropertyAction PropertyName="Text"
								IsEnabled="{Binding InputMode, Converter={StaticResource InReadKeyModeConverterRelay},
									Mode=OneWay}"
								Value="{Binding Prompt, Converter={StaticResource NullToEmptyStringConverterRelay}, 
									Mode=OneWay}" />
						</n:KeyTrigger>-->
						<i:EventTrigger>
							<n:SetFocusAction/>
						</i:EventTrigger>
					</i:Interaction.Triggers>
					<i:Interaction.Behaviors>						
						<n:BoolValueVisibilityBehavior Value="{Binding DisableInput, Mode=OneWay}" Negate="True"/>
						<Behaviors:TextBoxPrefixBehavior Prefix="{Binding Prompt, Mode=OneWay}"/>
						<Behaviors:SetKeyEventHandledBehavior SetIsHandled="{Binding InputMode, Converter={StaticResource InReadKeyModeConverterRelay}, Mode=OneWay}" KeyEvent="KeyDown"/>
					</i:Interaction.Behaviors>
				</TextBox>
			</StackPanel>
		</ScrollViewer>
        
		<StackPanel HorizontalAlignment="Left" VerticalAlignment="Top" Orientation="Horizontal" MaxWidth="500" d:IsLocked="True">
			<Border Height="30" Margin="0" 
				BorderThickness="10,0,0,0" BorderBrush="#FF595959" d:LayoutOverrides="Width">
				<TextBlock Text="{Binding Title, Mode=OneWay}" TextWrapping="Wrap"
					HorizontalAlignment="Left" VerticalAlignment="Center" 
					Margin="5,0,10,0" FontSize="13.333" Foreground="{Binding ForegroundColor, Converter={StaticResource ConsoleColorValueConverter}, Mode=OneWay}"/>
			</Border>
		</StackPanel>
        
        <!-- Beep sound from http://www.pacdv.com/sounds/interface_sounds-3.html-->
		<MediaElement x:Name="BeepAudioMedia" HorizontalAlignment="Left" VerticalAlignment="Top" 
			Source="/Orktane.Console;component/Resources/beep.wma" Volume="1" AutoPlay="False"/>
        
	</Grid>
    
</UserControl>
